package com.jwgf.itemviewdelegates;

import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;

import com.jwgf.adapter.ItemViewDelegate;
import com.jwgf.adapter.RecyclerViewHolder;
import com.jwgf.common.MD5;
import com.jwgf.common.ZipUtils;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.ShiftReport;
import com.jwgf.objectbox.ShiftReport_;
import com.jwgf.qrcode.QRCodeUtil;
import com.jwgf.qrreport.QRReport;
import com.jwgf.smarowindingmachine_unity.R;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;

public class ImageDelegate implements ItemViewDelegate {
    private LruCache<Long, Bitmap> mLruCache = new LruCache<>(8 * 1024 * 1024);

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_image;
    }

    @Override
    public boolean isForViewType(Object item, int position) {
        return true;
    }

    @Override
    public void bindView(RecyclerViewHolder holder, Object o, int position) {
        Long start = (Long) o;
        holder.setImageBitmap(R.id.image, load(start));
    }

    private Bitmap load(Long start) {
        Bitmap bitmap = mLruCache.get(start);
        if (bitmap == null) {
            Log.d("LruCache缓存", "没有缓存，开始创建新数据资源并缓存之。");
            StringBuilder qrContent = new StringBuilder();
            qrContent.append("http://101.200.182.225:5010/OfflineImport/AndroidHmi?");
            qrContent.append("uid=").append(MD5.readLine("//data//serial")).append("&");
            try {
                qrContent.append("report=").append(URLEncoder.encode(getCompressedContent(start), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            bitmap = QRCodeUtil.createQRCodeBitmap(qrContent.toString(), 400, 400, "L");
        }
        return bitmap;

    }

    private String getCompressedContent(Long start) {
        Box<ShiftReport> box = ObjectBox.get().boxFor(ShiftReport.class);
        List<ShiftReport> shiftReports = box.query().equal(ShiftReport_.start, start).build().find();
        List<QRReport.SpindleData> list = new ArrayList<>();
        for (ShiftReport item: shiftReports) {
            QRReport.SpindleData spindleData = QRReport.SpindleData.newBuilder()
                    .setType(item.type)
                    .setWindingTime(item.windingTime)
                    .setCopChangeCount(item.copChangeCount)
                    .setYarnJointCount(item.yarnJointCount)
                    .setYarnJointCountPer10Km(item.yarnJointCountPer10Km)
                    .setYarnJointCountPerPackage(item.yarnJointCountPerPackage)
                    .setCutCount(item.cutCount)
                    .setCutCountPer10Km(item.cutCountPer10Km)
                    .setCutCountPerCop(item.cutCountPerCop)
                    .setCutCountPerPackage(item.cutCountPerPackage)
                    .setPackageCount(item.packageCount)
                    .setSucceedDoffingPackageCount(item.succeedDoffingPackageCount)
                    .setDoffingTime(item.doffingTime)
                    .setSpliceCount(item.spliceCount)
                    .setSpliceTime(item.spliceTime)
                    .setSplicesCountRightFirstTime(item.splicesCountRightFirstTime)
                    .setProduction(item.production)
                    .setRunEfficiency(item.runEfficiency)
                    .setMechanicalEfficiency(item.mechanicalEfficiency)
                    .build();
            list.add(spindleData);
        }
        QRReport.WinderMachineData data = QRReport.WinderMachineData.newBuilder()
                .setShiftNum(shiftReports.get(0).shiftNum)
                .setStart(shiftReports.get(0).start)
                .setEnd(shiftReports.get(0).end)
                .addAllSpindles(list).build();
        try {
            return ZipUtils.compressToBase64(data.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
